home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 076-100 / disk_077 / samples / hanoi.d < prev    next >
Text File  |  1992-05-06  |  766b  |  39 lines

  1. #include:util.g
  2.  
  3. /*
  4.  * simple program to solve the "Towers of Hanoi" problem.
  5.  */
  6.  
  7. bool Print;
  8. int N;
  9.  
  10. proc hanoi(*char fromPeg, usingPeg, toPeg; int n)void:
  11.     int i;
  12.  
  13.     if n ~= 0 then
  14.     hanoi(fromPeg, toPeg, usingPeg, n - 1);
  15.     if Print then
  16.         for i from 1 upto N - n do
  17.         write(' ');
  18.         od;
  19.         writeln("Move disk ", n, " from ", fromPeg,
  20.             " peg to ", toPeg, " peg.");
  21.     fi;
  22.     hanoi(usingPeg, fromPeg, toPeg, n - 1);
  23.     fi;
  24. corp;
  25.  
  26. proc main()void:
  27.     channel input text chin;
  28.     char ch;
  29.  
  30.     open(chin, GetPar());
  31.     if read(chin; ch) and (ch = '-' or ch = '+') and
  32.         read(chin; N) and N >= 0 then
  33.     Print := ch = '-';
  34.     hanoi("left", "middle", "right", N);
  35.     else
  36.     writeln("Use is: hanoi {-|+}n   where n is \# of disks (>= 0)");
  37.     fi;
  38. corp;
  39.